package site.hanzhe.did.props;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import site.hanzhe.did.constant.DidConst;

import java.net.InetSocketAddress;
import java.net.Proxy;

/**
 * config.json 配置文件实体类
 *
 * @author 张涵哲
 * @since 2024-06-14 12:39:05
 */
@Getter
@Setter
@ToString
public class ConfigProp {

    private String proxy;
    private String httpProxy;
    private String socketProxy;

    /**
     * 创建默认对象
     *
     * @author 张涵哲
     * @since 2024-06-14 12:42:04
     */
    public static ConfigProp newDefaultInstance() {
        ConfigProp config = new ConfigProp();
        config.setProxy(DidConst.NONE);
        config.setHttpProxy("127.0.0.1:7897");
        config.setSocketProxy("127.0.0.1:7897");
        return config;
    }

    /**
     * 是否启用了代理
     *
     * @author 张涵哲
     * @since 2024-06-14 13:24:01
     */
    public boolean isEnableProxy() {
        return !DidConst.NONE.equals(this.proxy);
    }

    /**
     * 获取代理类型对象
     *
     * @author 张涵哲
     * @since 2024-06-14 13:24:08
     */
    public Proxy.Type getProxyType() {
        if (this.isEnableProxy()) {
            return DidConst.HTTP.equals(this.proxy) ? Proxy.Type.HTTP : Proxy.Type.SOCKS;
        }
        throw new RuntimeException("未启用代理，无法获取代理类型");
    }

    /**
     * 获取代理地址URL
     *
     * @author 张涵哲
     * @since 2024-06-14 13:24:13
     */
    public String getProxyUrl() {
        switch (this.getProxyType()) {
            case HTTP: return this.httpProxy;
            case SOCKS: return this.socketProxy;
        }
        return null;
    }

    /**
     * 获取代理地址Inet对象
     *
     * @author 张涵哲
     * @since 2024-06-14 13:24:13
     */
    public InetSocketAddress getProxyInet() {
        String[] proxyUrlSplit = getProxyUrl().split(":");
        return new InetSocketAddress(proxyUrlSplit[0], Integer.parseInt(proxyUrlSplit[1]));
    }

}
